package net.mof.manual.meta

import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.TypeName
import net.mof.manual.MarkedClassInfo
import net.mof.manual.Utils.jsonObject
import net.mof.manual.Utils.stringClassName
import net.mof.manual.Utils.textUtilsClassName
import net.mof.manual.Utils.upFirstChar

/**
 *
 * 字符串类型
 *
 * Created by LiYueHui on 2020/2/2.
 */
class StringFieldMetaInfo : BaseFieldMetaInfo() {
    override fun optField(): String = "String"

    override fun instanceTypeName(): TypeName = stringClassName

    override fun jsonToTypeGet(builder: CodeBlock.Builder, markedClassInfo: MarkedClassInfo) {
        builder.beginControlFlow("if(!\$T.isEmpty(\$L))", textUtilsClassName,
                value)
                .addStatement("\$L.set\$L(\$L)",
                        markedClassInfo.getFileName()?.toLowerCase(),
                        upFirstChar(filedName()), value).endControlFlow()
    }

    override fun typeToJsonObjectPut(builder: CodeBlock.Builder?, param: String?) {
        builder?.addStatement("\$T \$L = \$L.get\$L()", stringClassName, value,
                param, upFirstChar(filedName()))
        builder?.beginControlFlow("if(!\$T.isEmpty(\$L))", textUtilsClassName,
                value)
        builder?.addStatement("\$L.put(\$S,\$L)", jsonObject, value, value)
        builder?.endControlFlow()
    }
}